#!/usr/bin/python
# -*- coding: UTF-8 -*-
from model import anagraphdata
from model import anagraphdistance
import graphgenerator
import nodecluster
import sys
import json
import storagedistance

reload(sys)
sys.setdefaultencoding('utf8')

class DrawGraph:
    def __init__(self, algo_type = 1, distance_date = 0, similarity_gate = 0):
        self.gg             = graphgenerator.GraphGenerator(reshapeData = True, algorithm_type = algo_type)
        self.ndcluster      = nodecluster.NodeCluster(distance_gate = distance_date, similarity_gate = similarity_gate, algorithm_type = algo_type)
        self.ana            = anagraphdata.AnagraphData()

        self.export_path    = './data'
        self.file_name      = ''
        self.ana_list       = []

        self.gg.setDistanceGate(distance_date)

    def setFileName(self, file_name):
        if file_name:
            self.file_name = self.export_path + file_name + '.gexf'

    def constructGraph(self):
        # 初始化聚类数据
        self.ndcluster.setClusterNodes(self.ana_list)
        # 获取聚类后的数据
        ana_dict = self.ndcluster.startCluster()

        # 生成图
        self.gg.generateDictGraph(self.ana_list, ana_dict)
        # 设置输出文件名
        self.gg.setExportPath(self.file_name)
        # 导出图数据
        self.gg.exportData()

    # 设置初始化方剂数据
    def initData(self, ana_names = []):
        # 初始化节点数据
        for one_ana_name in ana_names:
             tmp_extend_list = self.ana.getAnagraphs(one_ana_name, ['name', 'medicines'])
             # 添加集合
             self.ana_list.extend(tmp_extend_list)

# 初始化类
obj_dg = DrawGraph(algo_type = 3, distance_date = 0.4, similarity_gate = 0.9)

# 设置输出文件名
obj_dg.setFileName('Jacarrd-伤寒-金匮-肘后-千金')
# 初始化数据 e: ['伤寒杂病论', '金匮要略', '肘后备急方', '备急千金要方']
obj_dg.initData(['伤寒杂病论', '金匮要略', '肘后备急方', '备急千金要方'])
# 开始构建
obj_dg.constructGraph()


# obj_ana = anagraphdata.AnagraphData()
# ana_list = obj_ana.getAnagraphs('伤寒杂病论', ['name', 'medicines'])
#
# obj_ndcluster = nodecluster.NodeCluster(distance_gate = 0.4, similarity_gate = 0.9, algorithm_type = 3)
# obj_ndcluster.setClusterNodes(ana_list)
# ana_dict = obj_ndcluster.startCluster()
#
# drawGraph(ana_list, ana_dict, 'Jacarrd-伤寒-0.6')
#
# # 添加金匮数据
# append_list = []
# append_list = obj_ana.getAnagraphs('金匮要略', ['name', 'medicines'])
# # 增加数据
# ana_list.extend(append_list)
# # 开始聚类
# obj_ndcluster.setClusterNodes(ana_list)
# ana_dict = obj_ndcluster.startCluster()
#
# drawGraph(ana_list, ana_dict, 'Jacarrd-伤寒-金匮-0.6')
#
# # 添加肘后数据到图中
# append_list = []
# # 获取指定方剂来源
# append_list = obj_ana.getAnagraphs('肘后备急方')
# # 添加数据
# obj_ndcluster.appendNodes(append_list)
# # 进行追加聚类
# append_dict = obj_ndcluster.appendCluster()
# # 获取所有节点
# node_list = obj_ndcluster.getNodes()
# # 指定输出文件名称
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-肘后-0.6')
#
# # 添加肘后数据到图中
# append_list = []
# # 获取指定方剂来源
# append_list = obj_ana.getAnagraphs('备急千金要方')
# # 添加数据
# obj_ndcluster.appendNodes(append_list)
# # 进行追加聚类
# append_dict = obj_ndcluster.appendCluster()
# # 获取所有节点
# node_list = obj_ndcluster.getNodes()
# # 指定输出文件名称
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-肘后-千金-0.6')
